package com.incito.crawl.common.utils.http;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.io.IOException;
import java.util.Properties;

/**
 * Created by wuyu on 2014/7/14.
 */
public class CrawlHttpClientConfig {
    private static Logger logger = LoggerFactory.getLogger(CrawlHttpClientConfig.class);
    private static boolean useProxy;
    private static int socketTimeout;
    private static int connectTimeout;
    private static String baseProxyUrl;

    public static final String PROXY_USE_COUNT = "proxyUseCount";

    static {
        Properties properties = null;
        try {
            properties = PropertiesLoaderUtils.loadProperties(new ClassPathResource("config/crawlhttpclient.properties"));
        } catch (IOException e) {
            e.printStackTrace();
            properties = new Properties();
        }


        useProxy = "true".equalsIgnoreCase(properties.getProperty("useproxy", "true")) ? true : false;

        try {
            socketTimeout = Integer.parseInt(properties.getProperty("socket_timeout", "5000"));
        } catch (NumberFormatException e) {
            socketTimeout = 5000;
        }

        try {
            connectTimeout = Integer.parseInt(properties.getProperty("connection_timeout", "3000"));
        } catch (NumberFormatException e) {
            connectTimeout = 3000;
        }

        baseProxyUrl = properties.getProperty("base_proxy_url", "http://proxy.tuan800-inc.com/proxymanage/proxy/getVerifyStringWithName.json?name=");
        baseProxyUrl = baseProxyUrl.trim();
        if (baseProxyUrl.endsWith(".com")) {
            baseProxyUrl = baseProxyUrl + "/proxymanage/proxy/getVerifyStringWithName.json?name=";
        }

    }

    public static boolean getUseProxy() {
        return useProxy;
    }

    public static int getSocketTimeout() {
        return socketTimeout;
    }

    public static int getConnectTimeout() {
        return connectTimeout;
    }

    public static String getBaseProxyUrl() {
        return baseProxyUrl;
    }
}
